

option explicit

class CWorkspaceUI

    private m_lPointer
	private m__oDictIncomingWorks


    private sub m_SetMeInGlobal()
        call Global.WUI_SetPointer(m_lPointer)
    end sub


    ' --------------------------------------------------------------------------
    ' Eventos
    public sub Class_Initialize()
'        m_lPointer = Global.WUI_GetPointer()
		set m__oDictIncomingWorks = createobject("scripting.dictionary")
    end sub

    public sub Class_Terminate()
		set m__oDictIncomingWorks = nothing
    end sub


    ' --------------------------------------------------------------------------
    ' Metodos
    public property get lPointer()
        lPointer = m_lPointer
    end property

    public property let lPointer(byval l)
        m_lPointer = l
    end property


	' manejo de incoming works
	public function AddIncomingWork()
        m_SetMeInGlobal
        AddIncomingWork = Global.WUI_AddIncomingWork()		
	end function 

	public function RemoveIncomingWorks()
        m_SetMeInGlobal
        m__oDictIncomingWorks.RemoveAll
        RemoveIncomingWorks = Global.WUI_RemoveIncomingWorks()		
	end function 

	public sub SetUserName(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetUserName(s)
	end sub

	public sub SetUserProfile(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetUserProfile(s)		
	end sub

	public property let sIncomingWorkId(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetIncomingWorkId(s)
	end property

	public property let sIncomingWorkClass(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetIncomingWorkClass(s)		
	end property

	public property let sIncomingWorkDescription(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetIncomingWorkDescription(s)		
	end property

	public property let sIncomingWorkDate(byval s)
        m_SetMeInGlobal
        call Global.WUI_SetIncomingWorkDate(s)		
	end property

	public property get sIncomingWorkId()
        m_SetMeInGlobal
        sIncomingWorkId = Global.WUI_GetIncomingWorkId()		
	end property

	public property get sIncomingWorkClass()
        m_SetMeInGlobal
        sIncomingWorkClass = Global.WUI_GetIncomingWorkClass()		
	end property

	public property get sIncomingWorkDescription()
        m_SetMeInGlobal
        sIncomingWorkDescription = Global.WUI_GetIncomingWorkDescription()		
	end property

	public property get sIncomingWorkDate()
        m_SetMeInGlobal
        sIncomingWorkDate = Global.WUI_GetIncomingWorkDate()		
	end property

	public sub TaskbarNotifyIncomingWork()
        m_SetMeInGlobal
        call Global.WUI_TaskbarNotify()				
	end sub
	
	public sub RefreshIncomingWorks()

		' Considerar que pueden haber incomming works nuevos e incomming works que deban eliminarse.
		' Pasos:
		'	Registro los incomming works que existen ahora, para luego saber cuales son nuevos y cuales no.
		'	Elimino todos los incomming works de WUI
		'	Traigo nuevamente los incomming works de la base de datos, ejecutando taskbar notification para los nuevos.

		dim oView
		dim oDictIncommingWorks
		dim i, oLstKeys
	
		' Replico los incoming works antes de eliminar todo
		' Esto se hace para saber cuales son nuevo.
		set oDictIncommingWorks = createobject("scripting.dictionary")
		oLstKeys = m__oDictIncomingWorks.keys
		for i=0 to m__oDictIncomingWorks.count-1 step 1
			oDictIncommingWorks.add oLstKeys(i), m__oDictIncomingWorks.item(oLstKeys(i))
		next

		' Elimino todos los actuales antes de agregarlos otra vez
		RemoveIncomingWorks

		' Agrego los incming works
		set oView = newviewhnd("web_public_incoming_work", "w")
		oView.sQuery = "select o.id as Id, c.name as ClassName, c.description as Description, o.numero as Number, o.fecha as Date from web_public_incoming_work w join qclass c on w.id_qclass = c.id join qerp_otp o on w.id_otp = o.id where o.id like '"& Global.sLoginCompany &"-%' and o.id_estado='1' order by o.fecha asc"
				
		oView.open
		if oView.IsOpen then
			if not oView.IsEof then 
				oView.movefirst
				do while not oView.IsEOF()
						
					' incoming works
					oApp.oUI.oWorkspaceUI.sIncomingWorkId = oView.sFieldValue("Id")
					oApp.oUI.oWorkspaceUI.sIncomingWorkClass = oView.sFieldValue("ClassName")
					oApp.oUI.oWorkspaceUI.sIncomingWorkDescription = oApp.oTranslator.Translation(oView.sFieldValue("Description")) & " " & oView.sFieldValue("Number")
					oApp.oUI.oWorkspaceUI.sIncomingWorkDate = left(oView.sFieldValue("Date"),4) & "-" & left(right(oView.sFieldValue("Date"),4),2) & "-" & right(oView.sFieldValue("Date"),2)
					oApp.oUI.oWorkspaceUI.AddIncomingWork
					
					m__oDictIncomingWorks.add oApp.oUI.oWorkspaceUI.sIncomingWorkId, oApp.oUI.oWorkspaceUI.sIncomingWorkId
								
					' taskbar notify, solo si este objeto es totalmente nuevo
					if not oDictIncommingWorks.exists(oApp.oUI.oWorkspaceUI.sIncomingWorkId) then
						call TaskbarNotifyIncomingWork()
					end if
					
					oView.movenext
				loop 
		
				oView.close
				oApp.oStorage.FreeView oView
			end if
		end if
	end sub


	' manejo de clases
    public function AddFolder(byval sFolderName, byval sFolderDescription)
        m_SetMeInGlobal
        call Global.WUI_SetFolderName(sFolderName)
        AddFolder = Global.WUI_AddFolder(sFolderDescription)
    end function

    public function AddClass(byval sClassName, byval sClassDescription, byval sFolderName, byval iIcon)
        m_SetMeInGlobal
        call Global.WUI_SetFolderName(sFolderName)
        call Global.WUI_SetClassName(sClassName)
        call Global.WUI_SetIcon(iIcon)
        AddClass = Global.WUI_AddClass(sClassDescription)
    end function

    public function iClassIndex(byval sClassName)
        m_SetMeInGlobal
        call Global.WUI_SetClassName(sClassName)
        iClassIndex = Global.WUI_GetClassIndex()
    end function
    
    public function RemoveClass(byval iClassIndex)
        m_SetMeInGlobal
        RemoveClass = Global.WUI_RemoveClass(iClassIndex)
    end function

	' eventos
    public sub SetOnClickEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnClickEvent(sFunctionName)
    end sub

    public sub SetOnDblClickEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnDblClickEvent(sFunctionName)
    end sub

    public sub SetOnGetFocusEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnGetFocusEvent(sFunctionName)
    end sub

    public sub SetOnLostFocusEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnLostFocusEvent(sFunctionName)
    end sub

    public sub SetOnSelectClassEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnSelectClassEvent(sFunctionName)
    end sub

    public sub SetOnSelectIncomingWorkEvent(byval sFunctionName)
        m_SetMeInGlobal
        call Global.WUI_SetOnSelectIncomingWorkEvent(sFunctionName)
    end sub

    public sub Refresh()
        m_SetMeInGlobal
        
        RefreshIncomingWorks
        call Global.WUI_Refresh()
    end sub

end class
